--[[
    --必须参数--
    stencil:模板
    content:裁剪内容

    --可选参数--
    parent:父节点
    pos:clipping相对于父节点位置
    contentPos:裁剪内容相对于clippingnode的位置
]]

--local Click = require(SCRIPTS_DIR .. ".pubs.Click");
local CURRENT_MODULE = ...;
local dotindex = string.find(CURRENT_MODULE, "%.%w+$");
local currentpath = string.sub(CURRENT_MODULE, 1, dotindex-1);
local clickPath = string.format("%s.Click", currentpath);
local Click = require(clickPath);

local ClipWidget = class("ClipWidget");
function ClipWidget:ctor(args)
    local stencil = args.stencil;
    local content = args.content;

    local parent = args.parent or nil;
    local pos = args.pos or cc.p(0,0);
    local contentPos = args.contentPos or cc.p(0,0);

    local clippingNode = cc.ClippingNode:create();
    clippingNode:setStencil(stencil);--设置裁剪模板
    stencil:setTag(100);
    clippingNode:setInverted(false);--设置底板可见
    clippingNode:setAlphaThreshold(0.5);--设置绘制底板的Alpha值为0.5
    content:setPosition(contentPos);
    clippingNode:addChild(content);
    if parent then
        clippingNode:setPosition(pos);
        parent:addChild(clippingNode);
    end
    self.clippingNode = clippingNode;
    self.content = content;
end

function ClipWidget:setParent(par)
    par:addChild(self.clippingNode);
end

function ClipWidget:setPositon(pos)
    self.clippingNode:setPosition(pos);
end


function ClipWidget:setClickHandler(func)
    local click = Click.new({targetNode = self.content,delegate = self.clippingNode});
    click:clickHandler(func);
end

return ClipWidget;


--endregion
